//
//  RouteListTableViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/21.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "RouteListTableViewController.h"
#import "RouteListTableViewCell.h"
#import "RouteListModel.h"
#import "RouteDetailViewController.h"
@interface RouteListTableViewController ()
@property(nonatomic,strong)NSMutableArray *arr;
@end

@implementation RouteListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[RouteListTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self JSONData];
    
}

-(NSMutableArray *)arr{
    if(!_arr){
        self.arr=[NSMutableArray array];
    }
    return _arr;
}

-(void)JSONData{
    NSLog(@"%@",self.url);
    [LORequestManger GET:self.url success:^(id response) {
        NSDictionary *dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"trails"]) {
            RouteListModel *model=[RouteListModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *TableSampleIdentifier = @"acell";
//    RouteListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
//    if (cell == nil) {
//        cell = [[RouteListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    RouteListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    RouteListModel *model=self.arr[indexPath.row];
    [cell setModelvalue:model];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RouteDetailViewController *route=[RouteDetailViewController new];
    RouteListModel *model=self.arr[indexPath.row];
    NSString *str=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trail/%@?&device_type=2&trail_id=%@&app_version=4.1.5&api_version=1",model.ID,model.ID];
    route.detailurl=str;
    NSString *comment=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/%@/score?access_token=6c66b840-ed3b-480f-8eb1-9ca54b970e90&app_version=2.3.6&device_type=1&page=1&page_size=2",model.ID];
    route.commentUrl=comment;
    NSString *act=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v3/activities?&device_type=2&app_version=4.1.5&api_version=1&is_recent=1&page_size=2&trail_id=%@&cur_page=1&sort=0&keywords=",model.ID];
    route.actUrl=act;
    NSString *zjurl=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/travelog/trail/%@?&page_size=6&device_type=2&trail_id=818&app_version=4.1.5&api_version=1&page=1",model.ID];
    route.zjUrl=zjurl;
    NSString *xgUrl=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/trail/related/809?&device_type=2&trail_id=%@&app_version=4.1.5&api_version=1&access_token=",model.ID];
    route.xgUrl=xgUrl;
    route.total_star=model.trail_score;
    [self.navigationController pushViewController:route animated:YES];
}
//隐藏下个控制器tabBar
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
    self.navigationController.tabBarController.tabBar.alpha = 1;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
